<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\Brand;
use App\Http\Requests\StoreBrand;
use App\Http\Requests\UpdateBrand;

class BrandController extends Controller
{
    function __construct()
    {
        view()->share([
            '_brand'=>'am-active',
            '_shop'=>'am-in'
        ]);
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $brands = Brand::orderBy('sort_order')->paginate(3);
        return view('admin.shop.brand.index', compact('brands'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.brand.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreBrand $request)
    {

        Brand::create($request->all());
        return redirect('/admin/shop/brand')->with('success', '新增品牌成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $brand = Brand::find($id);
        return view('admin.shop.brand.edit', compact('brand'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateBrand $request, $id)
    {
        $brand = Brand::find($id);
        $brand->update($request->all());
        return redirect('/admin/shop/brand')->with('success', '编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Brand::destroy($id);

    }

    /***
     * @param Request $request改变属性
     */
    public function change_attr(Request $request)
    {
        $brand = Brand::find($request->id);
        $attr = $request->attr;
        $brand->$attr = !$brand->$attr;
        $brand->save();

    }

    /***
     * @param Request $request排序
     */
    public function sort_order(Request $request)
    {
        $brand = Brand::find($request->id);
        $brand->sort_order = $request->sort_order;
        $brand->save();

//        $brand->setFiled('sort_order',$sort_order);
    }

    /***
     * 多选删除
     */
    public function destroy_all(Request $request)
    {
        Brand::destroy($request->checked_id);
    }
}
